package com.wangdong.wdbase.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.wangdong.wdbase.AppManager;
import com.wangdong.wdbase.R;
import com.wangdong.wdbase.widget.ProgressWheel;


/**
 * 作者：wangdong on 2018/10/18 10:45
 * 邮箱：2100520956@qq.com
 */

public class BaseDhqcActivity extends AppCompatActivity implements AppManager.AppListener {
    protected View v;
    private ProgressWheel progressBar;
    private RelativeLayout rl_network;
    private Button bt_reload;
    private Context context;
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        AppManager.getInstance().removeListener(this);
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context=this;
        AppManager.getInstance().addListener(this);
    }


    public void startActivityNotFinish(Class<?> cls){
        startActivity(new Intent(context, cls));
    }
    public void startActivity(Class<?> cls){
        startActivity(new Intent(context, cls));
        finish();
    }
    public void startWebActivity(String url, int tag){
//        Intent intent=new Intent(context, WebActivity.class);
//        intent.putExtra("url",url);
//        intent.putExtra("tag",tag);
//        startActivity(intent);
    }
    public void startWebActivity(String url, String title){
//        Intent intent=new Intent(context, WebActivity.class);
//        intent.putExtra("url",url);
//        intent.putExtra("title",title);
//        startActivity(intent);
    }
    public void startActivity(Class<?> cls, Bundle bundle){
        Intent intent=new Intent(context, cls);
        intent.putExtras(bundle);
        startActivity(intent);
        finish();
    }
    public void startActivityNotFinish(Class<?> cls, Bundle bundle){
        Intent intent=new Intent(context, cls);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    /**
     * 这个方法需要传入一个显示时候的布局,
     * 需要在初始化中添加此方法
     * @param v  这个v是隐藏的v 布局,和要显示失败的下面布局成交替显示,不是用这个v来初始化的不要想错了
     */
    public void initProgress(View v){
        this.v=v;
        rl_network=(RelativeLayout) findViewById(R.id.rl_network);
        progressBar=(ProgressWheel) findViewById(R.id.progressBar);
        bt_reload=(Button) findViewById(R.id.bt_reload);
        String text="请<font color='"+getResources().getColor(R.color.blue)+"'>刷新</font>或者<font color='"+getResources().getColor(R.color.blue)+"'>重试</font>";
        bt_reload.setText(Html.fromHtml(text));
        bt_reload.setOnClickListener(new networkClickListener());
    }

    /**
     * 加载时候显示进度
     */
    public void progress(){
        v.setVisibility(View.GONE);
        rl_network.setVisibility(View.GONE);
        progressBar.setVisibility(View.VISIBLE);
    }

    /**
     * 网络断了或者没有加载到数据显示
     */
    public void network(){
        v.setVisibility(View.GONE);
        rl_network.setVisibility(View.VISIBLE);
        progressBar.setVisibility(View.GONE);
    }
    /**
     * 这个是加载数据成功显示
     * */
    public void success(){
        v.setVisibility(View.VISIBLE);
        rl_network.setVisibility(View.GONE);
        progressBar.setVisibility(View.GONE);
    }
    /**
     * 这个是没有网络后刷新网络使用*/
    public void loadNetwork(){

    }

    /**这个通过AppManager发送信息接收使用*/
    @Override
    public void update(Object obj, String tag) {

    }

    public class networkClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            loadNetwork();
        }

    }
}
